package babo.cd.physicalexperiment.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.bumptech.glide.Glide;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.lxj.xpopup.core.BottomPopupView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import babo.cd.physicalexperiment.R;
import babo.cd.physicalexperiment.data.MyEnventData;
import babo.cd.physicalexperiment.utils.Out;
import babo.cd.physicalexperiment.view.RoundAngleImageView;

public class TiaozhengmkexDialog extends BottomPopupView {

    Context context;

    TextView tx_sure,tx_cancle;
    RoundAngleImageView iv_cutiao,iv_weitiao;
    WheelView wheelView1,wheelView2;
    long ctime=0;
    private String ctz="0";
    private String wtz="0";

    public TiaozhengmkexDialog(@NonNull Context context) {
        super(context);
        this.context=context;

    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_tiaozhengmkex;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        tx_sure=(TextView)findViewById(R.id.tx_sure);
        tx_cancle=(TextView) findViewById(R.id.tx_cancle);
        wheelView1=(WheelView)findViewById(R.id.wh1);
        wheelView2=(WheelView)findViewById(R.id.wh2);
        List<String> cts = new ArrayList<>();
        List<String> wts = new ArrayList<>();
        for (int i = -100; i<100 ; i++) {
            cts.add(i+"");
        }
        for (int i = -1000; i <1000 ; i++) {
            wts.add((i)+"");
        }


        wheelView1.setAdapter(new ArrayWheelAdapter(cts));
        wheelView1.setCyclic(false);
        wheelView1.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
              Out.out("粗调："+index+"  ==="+wheelView1.getAdapter().getItem(index).toString());

              ctz=wheelView1.getAdapter().getItem(index).toString();
             if (System.currentTimeMillis()-ctime>1500){
                 //todo 执行
             }
             ctime=System.currentTimeMillis();

               wheelView2.setAdapter(new ArrayWheelAdapter(wts));


            }
        });

            wheelView2.setAdapter(new ArrayWheelAdapter(wts));


        wheelView2.setCyclic(false);

        wheelView2.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {

                if (System.currentTimeMillis()-ctime>1500){
                    //todo 执行
                }
                wtz=wheelView2.getAdapter().getItem(index).toString();
                ctime=System.currentTimeMillis();
            }
        });
//        String ctzname=ctz;
//        for (int i=0;i<cts.size();i++){
//            if (cts.get(i).equals(ctzname)){
//                wheelView1.setCurrentItem(i);
//                break;
//            }
//        }
//        String wtzname=wtz;
//        for (int i=0;i<wts.size();i++){
//            if (wts.get(i).equals(wtzname)){
//                wheelView2.setCurrentItem(i);
//                break;
//            }
//        }
        wheelView1.setCurrentItem(100);
        wheelView2.setCurrentItem(1000);
        iv_cutiao=(RoundAngleImageView)findViewById(R.id.iv_cutiao);
        iv_weitiao=(RoundAngleImageView)findViewById(R.id.iv_weitiao);
        tx_cancle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                dismiss();
            }
        });

//        if (!TextUtils.isEmpty(cturl)){
//            Glide.with(context).load(cturl).into(iv_cutiao);
//        }
//        if (!TextUtils.isEmpty(wturl)){
//            Glide.with(context).load(wturl).into(iv_weitiao);
//        }
        setLister();

    }

    private void setLister() {
        tx_sure.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {


                 Out.out("粗调="+ctz+"  微调="+wtz);

                EventBus.getDefault().post(MyEnventData.getInstance("tiaozhengmkex@"+ctz+"@"+wtz));

                dismiss();

            }
        });
    }


}
